home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / STRTOL.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  4KB  |  121 lines

  1. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  2. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  3. ** Rochester NH, 03867-2954, USA.
  4. */
  5.  
  6. /*-
  7.  * Copyright (c) 1990 The Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms are permitted
  11.  * provided that: (1) source distributions retain this entire copyright
  12.  * notice and comment, and (2) distributions including binaries display
  13.  * the following acknowledgement:  ``This product includes software
  14.  * developed by the University of California, Berkeley and its contributors''
  15.  * in the documentation or other materials provided with the distribution
  16.  * and in all advertising materials mentioning features or use of this
  17.  * software. Neither the name of the University nor the names of its
  18.  * contributors may be used to endorse or promote products derived
  19.  * from this software without specific prior written permission.
  20.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  21.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  22.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23.  */
  24.  
  25. #if defined(LIBC_SCCS) && !defined(lint)
  26. static char sccsid[] = "@(#)strtol.c    5.3 (Berkeley) 5/17/90";
  27. #endif /* LIBC_SCCS and not lint */
  28.  
  29. #include <limits.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <stdlib.h>
  33.  
  34.  
  35. /*
  36.  * Convert a string to a long integer.
  37.  *
  38.  * Ignores `locale' stuff.  Assumes that the upper and lower case
  39.  * alphabets and digits are each contiguous.
  40.  */
  41. long
  42. strtol(nptr, endptr, base)
  43.     const char *nptr;
  44.     char **endptr;
  45.     register int base;
  46. {
  47.     register char *s = nptr;
  48.     register unsigned long acc;
  49.     register int c;
  50.     register unsigned long cutoff;
  51.     register int neg = 0, any, cutlim;
  52.  
  53.     /*
  54.      * Skip white space and pick up leading +/- sign if any.
  55.      * If base is 0, allow 0x for hex and 0 for octal, else
  56.      * assume decimal; if base is already 16, allow 0x.
  57.      */
  58.     do {
  59.         c = *s++;
  60.     } while (isspace(c));
  61.     if (c == '-') {
  62.         neg = 1;
  63.         c = *s++;
  64.     } else if (c == '+')
  65.         c = *s++;
  66.     if ((base == 0 || base == 16) &&
  67.         c == '0' && (*s == 'x' || *s == 'X')) {
  68.         c = s[1];
  69.         s += 2;
  70.         base = 16;
  71.     }
  72.     if (base == 0)
  73.         base = c == '0' ? 8 : 10;
  74.  
  75.     /*
  76.      * Compute the cutoff value between legal numbers and illegal
  77.      * numbers.  That is the largest legal value, divided by the
  78.      * base.  An input number that is greater than this value, if
  79.      * followed by a legal input character, is too big.  One that
  80.      * is equal to this value may be valid or not; the limit
  81.      * between valid and invalid numbers is then based on the last
  82.      * digit.  For instance, if the range for longs is
  83.      * [-2147483648..2147483647] and the input base is 10,
  84.      * cutoff will be set to 214748364 and cutlim to either
  85.      * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
  86.      * a value > 214748364, or equal but the next digit is > 7 (or 8),
  87.      * the number is too big, and we will return a range error.
  88.      *
  89.      * Set any if any `digits' consumed; make it negative to indicate
  90.      * overflow.
  91.      */
  92.     cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
  93.     cutlim = cutoff % (unsigned long)base;
  94.     cutoff /= (unsigned long)base;
  95.     for (acc = 0, any = 0;; c = *s++) {
  96.         if (isdigit(c))
  97.             c -= '0';
  98.         else if (isalpha(c))
  99.             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  100.         else
  101.             break;
  102.         if (c >= base)
  103.             break;
  104.         if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
  105.             any = -1;
  106.         else {
  107.             any = 1;
  108.             acc *= base;
  109.             acc += c;
  110.         }
  111.     }
  112.     if (any < 0) {
  113.         acc = neg ? LONG_MIN : LONG_MAX;
  114.         errno = ERANGE;
  115.     } else if (neg)
  116.         acc = -acc;
  117.     if (endptr != 0)
  118.         *endptr = any ? s - 1 : nptr;
  119.     return (acc);
  120. }
  121.